package com.wondertek.mist;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.Toast;

import com.bjca.xinshoushu.SignatureAPI;
import com.bjca.xinshoushu.Interface.OnSignatureResultListener;
import com.bjca.xinshoushu.beans.DataObj;
import com.wondertek.mist.plugins.SignaturePlugin;
import com.wondertek.mist.utils.RUtil;



public class NotifySignActivity extends Activity implements OnClickListener{

	public static final String SIGNATURE_NOTIFY = "notifySign";
	public static final String BITMAP_NOTIFY = "notifyBitmap";
	private SignatureAPI api;
	private String name;
	private String card;
	private String tid;
	private String signType;
	private String callback;
	private String bsData;
	private ImageButton notifyButton;                                                                                 
	private ImageButton handleButton;
	private ImageButton deleteButton;
	private String params;
	private Bitmap notifyBitmap;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		setContentView(RUtil.layout(this,"activity_notify_sign"));
		
		Bundle bundle = getIntent().getExtras();


		name = bundle.getString(SignaturePlugin.MIST_NAME_EXTRA);
		card = bundle.getString(SignaturePlugin.MIST_CARD_EXTRA);
		signType = bundle.getString(SignaturePlugin.MIST_SIGNTYPE_EXTRA);
		tid = bundle.getString("tid");
		callback = bundle.getString(SignaturePlugin.MIST_CALLBACK_EXTRA);
		bsData = bundle.getString(SignaturePlugin.MIST_BSDATA_EXTRA);

		notifyButton = (ImageButton)findViewById(RUtil.id(this,"NotifySign"));
	    handleButton = (ImageButton)findViewById(RUtil.id(this,"Handle"));
		deleteButton = (ImageButton)findViewById(RUtil.id(this,"Delete"));
		
        notifyButton.setOnClickListener(this);
        handleButton.setOnClickListener(this);
        deleteButton.setOnClickListener(this);
		
		initSign();
		
	}

	private void initSign() {
		api = new SignatureAPI(this, null);
		api.addDataObj(new DataObj(13));
		api.addDataObj(new DataObj(14));
		api.addDataObj(new DataObj(16));
		if(tid != null && tid.equals("3002")) {
			api.addSignatureObj(20, "请"+name+"签署协议本人已阅读保险条款",1,name.length());
		} else {
			api.addSignatureObj(20, "请签署姓名",0,0);
		}
		api.setTID(tid); 
		Log.d("--------tid----------", tid);
		api.setData(16, "100007");//神行车保渠道号
		api.commit();
		String serial_string = 
				"{\"bjcaxssrequest\":{\"submitinfo\":[{\"username\":\""+name+"\",\"identitycardnbr\":\""+card+"\"}]}}";

		api.setData(13, bsData);
		api.setData(14, serial_string);

		
		final Context context=this;
		api.setOnSignatureResultListener(new OnSignatureResultListener() {
			
			

			@Override
			public void onSignatureResult(int arg0, Bitmap arg1) {
				
				notifyBitmap = arg1;
				
				if (notifyBitmap!=null) {
					notifyButton.setBackgroundResource(RUtil.drawable(context,"notifyqm_click"));
					
				}
				
					
			}
			
			@Override
			public void onDialogDismiss(int arg0) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void onDialogCancel(int arg0) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void onDataDeleted(boolean arg0) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void onBufferSaved(boolean arg0) {
				// TODO Auto-generated method stub
				
			}
		});
		
		
		
		

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(RUtil.menu(this,"notify_sign"), menu);
		return true;
	}

	@Override
	public void onClick(View v) {

		
		if(v.getId()==RUtil.id(this,"NotifySign" )){
			api.showSingleInputDialog(20);
		}else if(v.getId()==RUtil.id(this,"Handle" )){
			if (notifyBitmap!=null) {
				//处理数据
				params = api.getUploadDataGram();
				
				Log.d("----------signtid----------", params);
				Intent intent = new Intent(NotifySignActivity.this, SignaturePlugin.class);
				
				File f01 = new File(Environment.getExternalStorageDirectory(),"cvd03.png");
				
				try {
					if(!f01.exists()){
						f01.createNewFile();
					}
					
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				FileOutputStream fOut01 = null;
				try {
					fOut01 = new FileOutputStream(f01);
				} catch (FileNotFoundException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				notifyBitmap.compress(Bitmap.CompressFormat.PNG, 100, fOut01);
				
				String file_pathnotify = Environment.getExternalStorageDirectory().toString()+"/cvd03.png";
				
				try {
					fOut01.flush();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				try {
					fOut01.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
				intent.putExtra(SIGNATURE_NOTIFY, params);
				intent.putExtra(BITMAP_NOTIFY, file_pathnotify);
				setResult(RESULT_OK, intent);
				//重置API
				api.reset();
				finish();	
				
				
			} else {
	           //提示错误
				Toast.makeText(NotifySignActivity.this, "请签名", Toast.LENGTH_SHORT).show();
			}
		}else if(v.getId()==RUtil.id(this,"Delete" )){
			this.finish();
		}
		
     
		
	}

}
